using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using System.Threading.Tasks;
using System;
using Volo.Abp.Uow; // 添加事务单元工作命名空间

namespace Acme.BookStore.Car
{
    public class OrdersAppService : CrudAppService<
        Orders,
        OrdersDto,
        int,
        PagedAndSortedResultRequestDto,
        CreateUpdateOrdersDto>,
        IOrdersAppService
    {
        private readonly IFleetcarAppService _fleetcarAppService;
        private readonly IUnitOfWorkManager _unitOfWorkManager; // 添加工作单元管理器
        public OrdersAppService(IRepository<Orders, int> repository, IFleetcarAppService fleetcarAppService, IUnitOfWorkManager unitOfWorkManager)
            : base(repository)
        {
            _fleetcarAppService = fleetcarAppService;
            _unitOfWorkManager = unitOfWorkManager;
        }

        [UnitOfWork] // 标记事务属性
        public override async Task<OrdersDto> CreateAsync(CreateUpdateOrdersDto input)
        {
            using (var uow = _unitOfWorkManager.Begin(requiresNew: true)) // 开始新事务
            {
                try
                {
                    input.Status = true; // 自动设为已下单
                    input.OrderNo = string.Format(DateTime.Now.ToString("yyyyMMddHHmmssfff")); // 时间戳生成单号

                    var order = await base.CreateAsync(input);

                    // 获取当前小时
                    var hour = DateTime.Now.Hour;
                    string quyu = hour < 12 ? "上午" : "下午";
                    // 自动创建派车记录
                    var fleetcar = new CreateUpdateFleetcarDto
                    {
                        OrderId = order.Id,
                        // 其他字段可根据订单信息赋值
                        Shipper = order.Shipper,
                        ShipperAddress = order.ShipperAddress,
                        Consignee = order.Consignee,
                        ConsigneeAddress = order.ConsigneeAddress,
                        Goods = order.Goods,
                        PieceCount = order.PieceCount,
                        Declaration = order.Declaration,
                        CubicMeterss = order.CubicMeterss,
                        Weightk = order.Weightk,
                        Lengthm = order.Lengthm,
                        Widthm = order.Widthm,
                        Heightm = order.Heightm,
                        FreightAmount = order.FreightAmount,
                        ReturnRequired = order.ReturnRequired,
                        Quyu = quyu, // 视业务而定
                        CustomerNo = order.CustomerNo,
                        OrderNo = order.OrderNo,
                        Remarks = order.Remarks
                    };
                    await _fleetcarAppService.CreateAsync(fleetcar);
                    // 提交事务
                    await uow.CompleteAsync();
                    return order;
                }
                catch (Exception ex)
                {
                    // 发生异常时回滚事务
                    await uow.RollbackAsync();
                    throw;
                }
            }
        }
    }
} 